package com.andy.nicefi.action;

import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.andy.nicefi.bean.User;
import com.andy.nicefi.exception.LogException;
import com.andy.nicefi.service.UserService;
import com.andy.nicefi.util.Mail;
import com.andy.nicefi.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

public class LogAction extends ActionSupport implements SessionAware{
	
	private User user;
	SessionMap<String, Object> sessionmap;
	private UserService userService;
	
	private String message;
	
	public String execute(){
		try {
			userService.validateLogin(user);
			message = getText("login.success");
			return "success";
		} catch (LogException e) {
			message = e.getMessage();
			return "failed";
		} catch (Exception e) {
			e.printStackTrace();
			return "failed";
		}
	}
	
	public String Logout(){
		sessionmap.invalidate();
		return "success";
	}

	public void setSession(Map<String, Object> session) {
		sessionmap = (SessionMap<String, Object>)session;
		sessionmap.put("isSignon", Boolean.TRUE);
		sessionmap.put("user", user);
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public SessionMap<String, Object> getSessionmap() {
		return sessionmap;
	}

	public void setSessionmap(SessionMap<String, Object> sessionmap) {
		this.sessionmap = sessionmap;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
